from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from myapp.models import *
from decorator.decorator import check_login,merchant_login
from myapp.serializer import *
from order.serializer import *
import json
from .serializer import *


# 发布评论
class CommentModelSet(ModelViewSet):
    queryset = Comment.objects.all()
    serializer_class = CommentSer
    # @check_login
    def create(self, request, *args, **kwargs):
        uid = request.data.get("uid")
        content = request.data.get('content')
        oid = request.data.get('oid')
        print(uid,content,oid)
        try:

            midd = Order.objects.filter(id=oid).first()
            mid_id = midd.merchant_id
            print('middddddd', mid_id)
            user = User.objects.filter(id=uid).first()
            user = UserSer(user).data
            user = json.loads(json.dumps(user))
            # print(order)
            Comment.objects.create(user_id_id=uid,content=content,merchant_id_id=mid_id,order_id_id=oid, user_img=user['portrait_img'])

            return Response({
                "code": 200,
                'msg': "发布评论成功!"
            })

        except Exception as e:
            print("错误>>", e)
            return Response({
                "code": 500,
                "msg": "服务器错误！"
            })

class CommentReplySet(ModelViewSet):
    queryset = CommentReply.objects.all()
    serializer_class = CommentReplySer

    # 回复评论
    def create(self, request, *args, **kwargs):
        try:
            cid = request.data.get('cid')
            content = request.data.get('centent')
            mid = request.data.get("mid")
            uid = request.data.get('uid')
            print('评论id>>', cid)
            print("内容>>>", content)
            print('uid>>', uid)
            print("mid>>", mid)
            if uid:
                user = User.objects.filter(id=uid).first()
                user_img = user.portrait_img
                CommentReply.objects.create(content=content, user_img=user_img, comment_id=cid,user_id_id=uid)

            if mid:
                metr = Merchant.objects.filter(id=mid).first()
                metr_img = metr.portrait_img
                CommentReply.objects.create(content=content, user_img=metr_img, comment_id=cid, merchant_id_id=mid)

            return Response({
                'code': 200,
                'msg': "发布评论成功！"
            })

        except Exception as e:
            print('错误>>', e)
            return Response({
                "code": 500,
                'msg': "服务器错误！"
            })

# 回复信息数据
class CommentShowAPIView(APIView):
    def post(self, request):
        uid = request.data.get('uid')
        buid = request.data.get('cid')
        mid = request.data.get('mid')
        content = request.data.get("centent")

        # 判断是否是用户
        if uid:
            # 获取被回复评论的id
            comment_bei = Comment.objects.filter(id=buid).first()
            beihuifu = comment_bei.user_id_id

            # 获取被回复的名称
            user_na = User.objects.filter(id=beihuifu).first()
            num_user = user_na.account

            # 获取当前登录的用户
            user_name = User.objects.filter(id=uid).first()
            huifu = user_name.account

            # 获取回复的头像
            user = User.objects.filter(id=uid).first()
            user_img = user.portrait_img
            # 把数据添加到库里面
            CommentShow.objects.create(commentator=huifu, be_reviewed=num_user, content=content, user_img=user_img)

        # 判断是否是商家
        if mid:
            # 获取被回复评论的id
            comment_bei = Comment.objects.filter(id=buid).first()
            beihuifu = comment_bei.user_id_id

            # 获取被回复的名称
            user_na = User.objects.filter(id=beihuifu).first()
            num_user = user_na.account

            # 获取当前登录的用户
            merchant_name = Merchant.objects.filter(id=mid).first()
            mername = merchant_name.username

            # 获取回复的头像
            mername_image = Merchant.objects.filter(id=mid).first()
            mername_img = mername_image.portrait_img

            # 把数据添加到库里面
            CommentShow.objects.create(commentator=mername,be_reviewed=num_user, content=content,user_img=mername_img)

        return Response({
            "code": 200,
            "msg": "添加回复数据成功！",
        })

    # 删除数据
    def delete(self,request):
        id = request.data.get('id')

        CommentShow.objects.get(id=id).delete()
        return Response({
            "code": 200,
            'msg': "删除成功",
        })


# 获取该商家的评论以及回复
class Merchant_comment(APIView):
    @merchant_login
    def get(self, request):
        mid = request.user
        print('商家did>>', mid)
        mserchan = Merchant.objects.filter(username=mid).first()
        mid_id=mserchan.id
        comment = Comment.objects.filter(merchant_id_id=mid_id).all()
        comment_ser = CommentSer(comment,many=True).data

        if not comment:
            return Response({'msg':"没有评论"})
        else:
            comment = CommentShow.objects.all()
            comment_show_ser = CommentShowSer(comment, many=True)
            return Response({
                'code': 200,
                'msg': "获取评论回复数据成功！",
                "comment_treply": comment_show_ser.data,
                'data': comment_ser,
            })


# 获取用户订单对商家的评论信息以及回复
class CommentUser(APIView):
    def get(self,request):
        oid = request.query_params.get('oid')
        print('ooid>>', oid)
        order = Order.objects.filter(id=oid).first()
        mid = order.merchant_id
        print('mid', mid)
        comment = Comment.objects.filter(merchant_id_id=mid).all()
        print('mddmd', comment)
        comment_ser = CommentSer(comment,many=True).data

        if not comment:
            return Response({'msg':"没有评论"})
        else:
            comment = CommentShow.objects.all()
            comment_show_ser = CommentShowSer(comment, many=True)
            return Response({
                'code': 200,
                'msg': "获取评论回复数据成功！",
                "comment_treply": comment_show_ser.data,
                'data': comment_ser,
            })
